<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Index extends CI_Controller {

    public function __construct() {
        parent::__construct();
        
        $this->load->driver('cache', array('adapter' => 'apc', 'backup' => 'file'));
        $this->load->database();
        $this->load->library('user');
        session_start();

        //load edgeworld config
        $this->config->load('edgeworld', TRUE);
        $edgeWorld = $this->config->item('edgeworld');
        $this->user->token = $edgeWorld['bank_token'];
        $this->user->baseUrl = $edgeWorld['base_url'];
        
        //load dbconfig
        $dbConfig = $this->user->getDbConfig();
        $this->config->set_item('app', $dbConfig);

        if (isset($_SESSION['user'])) {
            $data = $_SESSION['user'];
            $this->user->id         = $data['id'];
            $this->user->name       = $data['name'];
            $this->user->allianceId = $data['allianceId'];
            $this->user->sampleUrl  = $data['sampleUrl'];
            $this->user->baseUrl    = $data['baseUrl'];
            $this->user->token      = $data['token'];
        }
        //var_dump($user);
        
    }
    /**
     * Index Page for this controller.
     *
     * Maps to the following URL
     * 		http://example.com/index.php/welcome
     * 	- or -  
     * 		http://example.com/index.php/welcome/index
     * 	- or -
     * Since this controller is set as the default controller in 
     * config/routes.php, it's displayed at http://example.com/
     *
     * So any other public methods not prefixed with an underscore will
     * map to /index.php/welcome/<method_name>
     * @see http://codeigniter.com/user_guide/general/urls.html
     */
    public function index() {        
        
        $txtSampleData = $this->input->get_post('sampleData');
        
        if (false !== $txtSampleData) {
            $this->user->parseUrl($txtSampleData);
            $_SESSION['user'] = $this->user->toArray();            
        }
        
        $data['sampleData'] = $txtSampleData;
        $data['user']       = $this->user->toArray();

        $this->load->view('index', $data);
    }

    public function getWorldPlayers() {
        if ($this->user->id != null) {
            echo $this->user->getWorldPlayers();
        }
        else {
            echo 'Data is not set';
        }        
    }
    
    public function getOpponentPlayers() {
        if ($this->user->id != null) {
            echo $this->user->getOpponentPlayers();
        }
        else {
            echo 'Data is not set';
        }        
        

    }
    
    public function getTeamPlayers() {
        if ($this->user->id != null) {
            echo $this->user->getTeamPlayers();
        }
        else {
            echo 'Data is not set';
        }        
        

    }
    
    private function _getUserAllianceId($userId) {
        $sql = "SELECT alliance_id FROM user WHERE id=$userId";
        $query  = $this->db->query($sql);
        $row = $query->row();
        
        if ($row) {
            return $row->alliance_id;
        }
        return null;
    }
    
    public function getAttackerAlliance() {
        echo json_encode($this->user->getAttackerAlliance());
    }
    
    public function getAllianceData() {
        $id = $this->input->get_post('id'); 
        if (false!=$id) {
            $allianceData = $this->user->getAlliance($id);
            $this->user->updateAlliance($allianceData);
            echo json_encode($allianceData);
        }
    }

    
    public function updatePlayer() {
        $id = $this->input->get_post('id');
        $src = $this->input->get_post('src');
        $data = $this->user->updatePlayer($id, $src);
        
        $data['res1'] = number_format($data['res1'] / 1000000, 1);
        $data['res2'] = number_format($data['res2'] / 1000000, 1);
        $data['res3'] = number_format($data['res3'] / 1000000, 1);
        $data['res4'] = number_format($data['res4'] / 1000000, 1);
        $data['damage_protection'] = ($data['damage_protection'])?'x':'';
        $data['online'] = ($data['online'])?'x':'';
        echo json_encode($data);
    }
    
    public function viewData() {
        $userId = $this->user->id;
		
		$edgeWorld = $this->config->item('edgeworld');
        $excludeAllianceId = $edgeWorld['team_id'];        
        
        if ($userId == null) {echo 'Data is not set';return ;}
        
        $minRes     = intval($this->input->get_post('min_res'));
        $lastUpdate = $this->input->get_post('lastupdate');
        $notDamageProtection = $this->input->get_post('not_damage_protection') == 'on';
        $offline    = $this->input->get_post('offline')  == 'on';
        $notFriend  = $this->input->get_post('not_friend')  == 'on';
        $sortBy     = $this->input->get_post('sort');
        
        $data = $this->user->browseData($minRes, $notDamageProtection, $offline, $notFriend, $lastUpdate, $excludeAllianceId, $sortBy);
        
        foreach ($data as &$item) {
            $item['res1'] = number_format($item['res1'] / 1000000, 1);
            $item['res2'] = number_format($item['res2'] / 1000000, 1);
            $item['res3'] = number_format($item['res3'] / 1000000, 1);
            $item['res4'] = number_format($item['res4'] / 1000000, 1);
            $item['damage_protection'] = ($item['damage_protection'])?'p':'';
            $item['online'] = ($item['online'])?'o':'';
        }
        
        $viewData = Array(
            'data' => $data,
            'min_res' => $minRes,
            'not_damage_protection' => $notDamageProtection,
            'online' => $offline,
            'friend' => $notFriend
        );
      
        $this->load->view('data2', $viewData);
    }
    
    public function updateTeam() {
        $this->user->updateTeam();
    }

    public function viewTeam() {
        $sort = $this->input->get_post('sort');
        
        $teamData = $this->user->getPlayerHistory($sort);
        
        //get bank data
        /*
        $edgeWorld = $this->config->item('edgeworld');
        $id = $edgeWorld['bank_id'];
        $bankData = $this->user->requestPlayer($id);
        */
        $appConfig = $this->config->item('app');
        $bankData = isset($appConfig['bank_data'])?json_decode($appConfig['bank_data']):null;
        //var_dump($bankData);die;
        
        $viewData = array(
            'players' => $teamData,
            'bank' => $bankData,
        );
        $this->load->view('team', $viewData);
    }
	
	public function fetchRequest() {
		
	}
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */